/************************************************************************************/
/*  Program name CAPAPEXT_AP_BANK_AC.sql                                            */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
ALTER SESSION SET NLS_DATE_FORMAT='YYYYMMDD HH24:MI:SS';
SET SERVEROUTPUT ON SIZE 1000000;
set verify off;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAPEXT_AP_BANK_AC';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	lv_FileName		VARCHAR2(100)	:=	'CAPAP_AP_BANK_AC.dat';
	lv_PathName		VARCHAR2(100)	:=	'/tmp';
	lv_UTL_File		UTL_FILE.File_Type;

	lv_SOB_SName	VARCHAR2(100);
	lv_SOB_Count	NUMBER;

	CURSOR Cur_F(p_NGO_SOB VARCHAR2) IS
	SELECT
		A.*,
		B.Short_Name	SOB_Short_Name
	FROM
		GL_SETS_OF_BOOKS		B,
		AP_BANK_ACCOUNTS_ALL	A
	WHERE	B.Short_Name		=	p_NGO_SOB
	AND		B.Set_Of_Books_ID	=	A.Set_Of_Books_ID;
--	AND		A.Account_Type		=	'INTERNAL';

BEGIN
	lv_SOB_SName	:=	UPPER('&1');

	SELECT	COUNT('A')
	INTO	lv_SOB_Count
	FROM	GL_SETS_OF_BOOKS
	WHERE	UPPER(Short_Name)	=	lv_SOB_SName;

	IF	lv_SOB_Count	>=	1	THEN
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName, 'w');
		FOR C1 IN Cur_F(lv_SOB_SName) LOOP

			UTL_FILE.PUT_LINE(
				lv_UTL_File,
				C1.Bank_Account_ID||CHR(9)||
				C1.Bank_Account_Name||CHR(9)||
				C1.Last_Update_Date||CHR(9)||
					C1.Last_Updated_By||CHR(9)||
				C1.Last_Update_Login||CHR(9)||
				C1.Creation_Date||CHR(9)||
					C1.Created_By||CHR(9)||
				C1.Bank_Account_Num||CHR(9)||
					C1.Bank_Branch_ID||CHR(9)||
					C1.Set_Of_Books_ID||CHR(9)||
				C1.Currency_Code||CHR(9)||
				C1.Description||CHR(9)||
				C1.Contact_First_Name||CHR(9)||
				C1.Contact_Middle_Name||CHR(9)||
				C1.Contact_Last_Name||CHR(9)||
				C1.Contact_Prefix||CHR(9)||
				C1.Contact_Title||CHR(9)||
				C1.Contact_Area_Code||CHR(9)||
				C1.Contact_Phone||CHR(9)||
				C1.Max_Check_Amount||CHR(9)||
				C1.Min_Check_Amount||CHR(9)||
				C1.One_Signature_Max_Flag||CHR(9)||
				C1.Inactive_Date||CHR(9)||
				C1.Avg_Float_Days||CHR(9)||
					C1.Asset_Code_Combination_ID||CHR(9)||
					C1.Gain_Code_Combination_ID||CHR(9)||
					C1.Loss_Code_Combination_ID||CHR(9)||
				C1.Bank_Account_Type||CHR(9)||
				C1.Validation_Number||CHR(9)||
				C1.Max_Outlay||CHR(9)||
				C1.Multi_Currency_Flag||CHR(9)||
				C1.Account_Type||CHR(9)||
				C1.Attribute_Category||CHR(9)||
				C1.Attribute1||CHR(9)||
				C1.Attribute2||CHR(9)||
				C1.Attribute3||CHR(9)||
				C1.Attribute4||CHR(9)||
				C1.Attribute5||CHR(9)||
				C1.Attribute6||CHR(9)||
				C1.Attribute7||CHR(9)||
				C1.Attribute8||CHR(9)||
				C1.Attribute9||CHR(9)||
				C1.Attribute10||CHR(9)||
				C1.Attribute11||CHR(9)||
				C1.Attribute12||CHR(9)||
				C1.Attribute13||CHR(9)||
				C1.Attribute14||CHR(9)||
				C1.Attribute15||CHR(9)||
				C1.Pooled_Flag||CHR(9)||
				C1.Zero_Amounts_Allowed||CHR(9)||
				C1.Request_ID||CHR(9)||
				C1.Program_Application_ID||CHR(9)||
				C1.Program_ID||CHR(9)||
				C1.Program_Update_Date||CHR(9)||
				C1.Receipt_Multi_Currency_Flag||CHR(9)||
				C1.Check_Digits||CHR(9)||
					C1.Org_ID||CHR(9)||
					C1.Cash_Clearing_CCID||CHR(9)||
					C1.Bank_Charges_CCID||CHR(9)||
					C1.Bank_Errors_CCID||CHR(9)||
					C1.Earned_CCID||CHR(9)||
					C1.Unearned_CCID||CHR(9)||
					C1.On_Account_CCID||CHR(9)||
					C1.Unapplied_CCID||CHR(9)||
					C1.Unidentified_CCID||CHR(9)||
					C1.Factor_CCID||CHR(9)||
					C1.Receipt_Clearing_CCID||CHR(9)||
					C1.Remittance_CCID||CHR(9)||
					C1.Short_Term_Deposit_CCID||CHR(9)||
				C1.Global_Attribute_Category||CHR(9)||
				C1.Global_Attribute1||CHR(9)||
				C1.Global_Attribute2||CHR(9)||
				C1.Global_Attribute3||CHR(9)||
				C1.Global_Attribute4||CHR(9)||
				C1.Global_Attribute5||CHR(9)||
				C1.Global_Attribute6||CHR(9)||
				C1.Global_Attribute7||CHR(9)||
				C1.Global_Attribute8||CHR(9)||
				C1.Global_Attribute9||CHR(9)||
				C1.Global_Attribute10||CHR(9)||
				C1.Global_Attribute11||CHR(9)||
				C1.Global_Attribute12||CHR(9)||
				C1.Global_Attribute13||CHR(9)||
				C1.Global_Attribute14||CHR(9)||
				C1.Global_Attribute15||CHR(9)||
				C1.Global_Attribute16||CHR(9)||
				C1.Global_Attribute17||CHR(9)||
				C1.Global_Attribute18||CHR(9)||
				C1.Global_Attribute19||CHR(9)||
				C1.Global_Attribute20||CHR(9)||
				C1.Bank_Account_Name_Alt||CHR(9)||
				C1.Account_Holder_Name||CHR(9)||
				C1.Account_Holder_Name_Alt||CHR(9)||
				C1.EFT_Requester_ID||CHR(9)||
				C1.EFT_User_Number||CHR(9)||
				C1.Payroll_Bank_Account_ID||CHR(9)||
					C1.Future_Dated_Payment_CCID||CHR(9)||
				C1.Edisc_Receivables_TRX_ID||CHR(9)||
				C1.Unedisc_Receivables_TRX_ID||CHR(9)||
					C1.BR_Remittance_CCID||CHR(9)||
					C1.BR_Factor_CCID||CHR(9)||
				C1.Br_Std_Receivables_TRX_ID||CHR(9)||
				C1.Allow_Multi_Assignments_Flag||CHR(9)||
				C1.Agency_Location_Code||CHR(9)||
				C1.IBAN_Number||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.Last_Updated_By)||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.Created_By)||CHR(9)||
				CAPBREXT_COMMON.GetBankName(C1.Bank_Branch_ID)||CHR(9)||
				C1.SOB_Short_Name||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Asset_Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Gain_Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Loss_Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetOrgName(C1.Org_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Cash_Clearing_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Bank_Charges_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Bank_Errors_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Earned_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Unearned_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.On_Account_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Unapplied_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Unidentified_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Factor_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Receipt_Clearing_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Remittance_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Short_Term_Deposit_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Future_Dated_Payment_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.BR_Remittance_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.BR_Factor_CCID)
			);

			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		UTL_FILE.FClose(lv_UTL_File);
	ELSE
		lv_Err_Count	:=	lv_Err_Count + 1;
		lv_Err_Msg		:=	lv_SOB_SName||': No such set of books exists';
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/